<?php

namespace iBrand\Store\Backend\Providers;

use iBrand\Store\Backend\Console\InitMenusCommand;
use iBrand\Store\Backend\Console\InstallCommand;
use iBrand\Store\Backend\Console\MenusCommend;
use iBrand\Store\Backend\Console\RefundCommand;
use iBrand\Store\Backend\Console\SetDefaultValueCommand;
use iBrand\Store\Backend\Console\SpecCommand;
use iBrand\Store\Backend\Console\StoreMenusCommand;
use iBrand\Store\Backend\Console\SystemSettingMenusCommand;
use iBrand\Store\Backend\Console\TravelCommand;
use iBrand\Store\Backend\Listeners\LogSuccessfulLoginListener;
use iBrand\Store\Backend\Model\Advertisement;
use iBrand\Store\Backend\Model\AdvertisementItem;
use iBrand\Store\Backend\Model\Product;
use iBrand\Store\Backend\Model\SeckillItem;
use iBrand\Store\Backend\Observers\AdvertisementItemObserver;
use iBrand\Store\Backend\Observers\AdvertisementObserver;
use iBrand\Store\Backend\Observers\ProductObserver;
use iBrand\Store\Backend\Observers\SeckillItemObserver;
use iBrand\Store\Backend\StoreBackend;
use iBrand\UEditor\UEditorServiceProvider;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use iBrand\Store\Backend\Service\GoodsService;
use iBrand\Store\Backend\Service\OrderService;
use iBrand\Store\Backend\Service\ExcelExportsService;
use iBrand\Store\Backend\Service\DiscountService;
use iBrand\Store\Backend\Service\RefundService;
use iBrand\Store\Backend\Service\RegistrationsService;
use Event;
use iBrand\Store\Backend\Http\Middleware\Bootstrap;

class BackendServiceProvider extends ServiceProvider
{
	protected $namespace = 'iBrand\Store\Backend\Http\Controllers';

	protected $subscribe = [
		//'iBrand\Store\Backend\Listeners\GoodsPurchaseEventListener',
		//'iBrand\Store\Backend\Listeners\RefundAuditEventListener',
		//'iBrand\Store\Backend\Listeners\WeChatMessageListener',
		//'iBrand\Store\Backend\Listeners\PromotionEventListener',
		//'iBrand\Store\Backend\Listeners\MultiGrouponRefundEventListener',
	];



	public function boot()
	{
		/*if (!app()->runningInConsole()) {

			foreach ($this->subscribe as $item) {
				Event::subscribe($item);
			}
		}*/


		$this->publishes([
			__DIR__ . '/../config.php' => config_path('store.php'),
			__DIR__ . '/../dmp.php'    => config_path('dmp.php'),
		]);

		if (!$this->app->routesAreCached()) {
			$this->mapWebRoutes();
		}

		$this->loadViewsFrom(__DIR__ . '/../../resources/views', 'store-backend');


		if ($this->app->runningInConsole()) {

			$this->publishes([
				__DIR__ . '/../../resources/assets'                 => public_path('assets/backend'),
				__DIR__ . '/../../resources/assets/template'        => public_path('assets/template'),
				__DIR__ . '/../../resources/assets/libs/sweetalert' => public_path('vendor/laravel-admin/sweetalert'),
			], 'store-backend-assets');
		}

		$this->commands([
			SpecCommand::class,
			MenusCommend::class,
			InitMenusCommand::class,
			SystemSettingMenusCommand::class,
			StoreMenusCommand::class,
		]);

	}

	public function register()
	{
		$this->app->register(UEditorServiceProvider::class);

		$this->app->singleton('GoodsService', function () {
			return new  GoodsService();
		});

		$this->app->singleton('OrderService', function () {
			return new  OrderService();
		});


		$this->app->singleton('DiscountService', function () {
			return new  DiscountService();
		});

		app('router')->aliasMiddleware('market.bootstrap', Bootstrap::class);
	}

	protected function mapWebRoutes()
	{
		Route::group([
			'middleware' => ['web', 'admin'],
			'namespace'  => $this->namespace,
		], function ($router) {
			require __DIR__ . '/../Http/routes.php';
		});
	}

	public function provides()
	{
		return ['GoodsService', 'OrderService', 'DiscountService', 'RefundService'];
	}
}
